package com.lovingtour.manager;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.lovingtour.manager.entity.Organization;
import com.lovingtour.manager.util.HttpUtil;

public class FWLXListActivity extends Activity {
	private EditText etSearch;
	private ImageButton ibSearch;
	private ListView lvList;
	private ArrayAdapter<String> adapter;
	private ArrayList<Organization> ogs;

	private Button btnOk;
	private String org;
	private ArrayList<Organization> orgs;
	private ImageButton btnAdd;

	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:
				ogs = (ArrayList<Organization>) JSON.parseArray(
						(String) (msg.obj), Organization.class);
				Log.i("info", "size==" + ogs.size());
				for (int i = 0; i < ogs.size(); i++) {
					adapter.add(ogs.get(i).getoName());
				}
				// adapter.notifyDataSetChanged();
				lvList.setAdapter(adapter);
				lvList.setItemsCanFocus(false);
				lvList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

				break;

			default:
				break;
			}

		};

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.servicetype_list);
		initView();
		initData();
		initListener();
	}

	private void initListener() {
		// TODO Auto-generated method stub

		ibSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (etSearch.getText().toString().trim() != null
						&& !"".equals(etSearch.getText().toString().trim())) {
					orgs = getSearchResult(etSearch.getText().toString().trim());
					adapter.clear();
					for (int i = 0; i < orgs.size(); i++) {
						adapter.add(orgs.get(i).getoName());
					}
					lvList.setAdapter(adapter);
					lvList.setItemsCanFocus(false);
					lvList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
				}
			}
		});
		lvList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				org = ((CheckedTextView) view).getText().toString().trim();
				Log.i("info", "org==" + org);
			}
		});

		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle b = new Bundle();
				b.putString("organization", org);
				Intent intent = new Intent();
				intent.putExtras(b);

				setResult(RESULT_OK, intent);
				finish();

			}
		});
		btnAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FWLXListActivity.this,
						AddServicetTypeActivity.class);

				startActivityForResult(intent, 1);
			}
		});

	}

	private void initData() {
		// TODO Auto-generated method stub
		adapter = new ArrayAdapter<String>(this, R.layout.fwjg_list_item);

		// adapter.
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				String result = HttpUtil.sendMsgToNet(FWLXListActivity.this,
						HttpUtil.getOrganizations, null);
				if (result != null) {
					Message msg = mHandler.obtainMessage();
					msg.what = 0;
					msg.obj = result;
					mHandler.sendMessage(msg);
				}
			}
		}).start();
	}

	private void initView() {
		// TODO Auto-generated method stub
		etSearch = (EditText) findViewById(R.id.edit_fwlx_search);
		ibSearch = (ImageButton) findViewById(R.id.ib_fwlx_search);
		lvList = (ListView) findViewById(R.id.lv_fwlx_list);
		btnOk = (Button) findViewById(R.id.btn_fwlx_choise_ok);
		btnAdd = (ImageButton) findViewById(R.id.fwlx_add_servicetype);
	}

	private ArrayList<Organization> getSearchResult(String search) {
		ArrayList<Organization> os = new ArrayList<Organization>();
		for (int i = 0; i < ogs.size(); i++) {
			if (ogs.get(i).getoName().contains(search)) {
				os.add(ogs.get(i));
			}
		}
		return os;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

}
